<?php
	/** Classe de gestion des images
	 * @author Sebastian Mosser
	 */
class Picture {

	private $_fileName;
	private $_data;
	
	public static $TAG_VALUE = 
				"Copyright OGC Nice Cote d'Azur [ http://ogcnice-association.fr ]";

	public function __construct($f) {
		$this->_fileName = $f;
		$this->_data = @getimagesize($f);
	}
	
	public function getExtension() {
		switch($this->_data[2]) {
			case 2 :  // JPG
				return ".jpg";
			case 1 :  //GIF
				return ".gif";
			case 3 :  // PNG
				return ".png";
			default: 
				return False;
		}
	}
	
	public function duplicate($fileName) {
		if (! file_exists($this->_fileName) )
			return False;
		$ans = copy($this->_fileName,$fileName);
		if ($ans)
			return new Picture($fileName);
		else
			return False;
	}
	
	public function delete() {
		if (file_exists($this->_fileName))
			unlink($this->_fileName);
	}
	
	public function resize($width,$height) {
		$gd = $this->openPicture();
		
		$result = imagecreatetruecolor($width,$height);
		
		imagecopyresampled($result,$gd,0,0,0,0,$width,$height,
											             $this->_data[0],$this->_data[1]);
		if (file_exists($this->_fileName))
			unlink($this->_fileName);
		$this->writePicture($result);
	}
	
	public function dumpInfo() {
		var_dump($this->_data);
	}
	
	public function tag($s) {
		// fonte de caractère utilisée :
		$font = 5;
		$fw = imagefontwidth($font);
		$fh = imagefontheight($font);
		
		// récupération des infos sur l'image
		$gd = $this->openPicture();
		$w = $this->_data[0];
		$h = $this->_data[1];
		
		// Longueur de la chaine de caractères
		$L = $fw * strlen($s) + 10;
		$l = $fh + 10;

		// Calcul des coordonées
		$x1 = $w - 10 - $L;
		$y1 = $h - 10 - $l;
		$x2 = $w - 10;
		$y2 = $h - 10;
		
		// Positionnement des couleurs
		$red   = imagecolorallocate($gd,255,0,0);
		$black = imagecolorallocate($gd,0,0,0);
		
		// Positionnement du tag 
		imagefilledrectangle($gd,$x1-5,$y1-5,$x2+5,$y2+5,$black);
		imagefilledrectangle($gd,$x1,$y1,$x2,$y2,$red);
		imagestring($gd,$font,$x1+5,$y1+5,$s,$black);

		// Ecriture
		$this->writePicture($gd);		
	}

	// On se base sur l'extension pour effectuer le chargement
	private function openPicture() {
		
		$gd = False;
		switch($this->_data[2]) {
			case 2 :  // JPG
				$gd = @imagecreatefromjpeg($this->_fileName);
				break;
			case 1 :  //GIF
				$gd = @imagecreatefromgif($this->_fileName);
				break;
			case 3 :  // PNG
				$gd = @imagecreatefrompng($this->_fileName);
				break;
		}
		if (! $gd) {
			$im = imagecreatetruecolor(150,30);
			$bgc = imagecolorallocate($im,255,255,255);
			$tc = imagecolorallocate($im,0,0,0);
			imagefilledrectangle($im,0,0,150,30,$bgc);
			imagestring($im,1,5,5,"Erreur de chargement !",$tc);
			$gd = $im;
		}
		return $gd;
	}

	private function writePicture($gd) {
		
		switch($this->_data[2]) {
			case 2 :  // JPG
				@imagejpeg($gd,$this->_fileName);
				break;
			case 1 :  //GIF
				@imagegif($gd,$this->_fileName);
				break;
			case 3 :  // PNG
				@imagepng($gd,$this->_fileName);
				break;
		}
		// On recharge les informations
		$this->_data = @getimagesize($this->_fileName);
	
	}

}


?>